<template>
  <el-dialog width="500px" v-bind="$attrs" @close="handleClose" @open="handleOpen">
    <el-form @submit.prevent.native label-position="top" :model="form" ref="form" :rules="rules">
      <el-form-item prop="name">
        <template #label class="startRequire"
          ><span>{{ $t('modules.pass.key_name') }}</span>
          <el-tooltip placement="top" :content="$t('flypass.nfc_public_key_name')">
            <svg-icon class="ml-2" icon-class="OutlinedQuestionCircle" />
          </el-tooltip>
        </template>
        <el-input
          maxlength="32"
          show-word-limit
          v-model="form.name"
          @blur="() => (form.name = form.name.trim())"
          :placeholder="$t('modules.pass.key_name')"
        ></el-input>
      </el-form-item>
      <el-form-item prop="key">
        <template #label class="startRequire"
          ><span>{{$t('modules.pass.public_key')}}</span>
          <el-tooltip placement="top" :content="$t('flypass.nfc_public_key_upload')">
            <svg-icon class="ml-2" icon-class="OutlinedQuestionCircle" />
          </el-tooltip>
        </template>
        <el-input
          type="textarea"
          maxlength="1024"
          show-word-limit
          v-model="form.key"
          @blur="() => (form.key = form.key.trim())"
          :placeholder="$t('modules.pass.public_key')"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('common.description') + ':'" prop="description">
        <el-input
          type="textarea"
          maxlength="256"
          show-word-limit
          v-model="form.description"
          @blur="() => (form.description = form.description.trim())"
          :placeholder="$t('common.description')"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button text @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="confirm()">{{
          $t('common.confirm')
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { addNFC } from '@/api/pass-generate'
export default {
  data() {
    return {
      confirmLoading: false,
      form: {
        name: '',
        key: '',
        description: ''
      }
    }
  },
  computed: {
    rules() {
      return {
        name: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('page.tip_enter')
          }
        ],
        key: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('page.tip_enter')
          }
        ]
      }
    }
  },
  methods: {
    handleOpen() {
      this.$refs['form'].resetFields()
    },
    handleClose() {
      this.$refs['form'].resetFields()
      this.$emit('update:model-value', false)
    },
    confirm() {
      this.$confirm(this.$t('modules.pass.add_file_tip'), this.$t('modules.pass.warning'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'info'
      })
        .then(() => {
          this.$refs['form'].validate((valid) => {
            if (valid) {
              this.confirmLoading = true
              let data = {
                keyName: this.form.name,
                // creUserId:''
                // characterCode:'',
                // keyId:'',
                publicKey: this.form.key,
                description: this.form.description
              }
              addNFC(data)
                .then((res) => {
                  this.confirmLoading = false
                  this.$emit('update:model-value', false)
                  this.$emit('refresh')
                  this.$message({
                    type: 'success',
                    message: this.$t('page.add_success')
                  })
                })
                .catch((err) => {
                  this.confirmLoading = false

                  this.$confirmError(err)
                })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('modules.pass.add_canceled')
          })
        })
    }
  }
}
</script>

<style scoped>
.startRequire ::before {
  content: '*';
  color: var(--el-color-danger);
  margin-right: 4px;
}
</style>
